import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    list: [],
    hasNextPage: false,
    keyword: '',
    showModal: false,
    address:[],
    pageData: {}
  },


  onShow: function() {
    var reason = this.selectComponent("#reason")
    this.getlist()
  },

  //查询列表
  getlist: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findSharedStore',
      method: 'POST',
      data: {
        keyword: this.data.keyword,
        disId: this.data.address.length > 2 ? this.data.address[2].code : '',
        cityId: this.data.address.length > 1 ? this.data.address[1].code : '',
        proId: this.data.address.length > 0 ? this.data.address[0].code : '',
        pageNo: this.data.pageNo
      }
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }
      this.setData({
        list: list,
        pageData: res.page,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

      console.log(res)

    })
  },


  //下拉刷新
  onPullDownRefresh: function() {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function() {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function() {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function(e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },
  seachInputVal: function(e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function() {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },

  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },

  popupConfirm: function () {
    this.getlist();
    this.popupCancel();
  },
  
  //重置
  popupReset: function () {
    this.setData({
      disId: '',
      address: [],
      quyu: false
    })
  },

  chooseAddress: function () {
    this.setData({
      quyu: true
    })
  },

  addresscancel: function () {
    this.setData({
      quyu: false
    })
  },

  addressDetail: function (e) {
    this.setData({
      address: e.detail,
      quyu: false
    })
  },
  //********************筛选更多end********************



  agreed: function(e) {
    var item = e.currentTarget.dataset.item
    var params = {
      "id": item.id,
      "stoId": item.stoId,
      "shared": item.shared,
      "status": 1,
      "auditMemo": "同意"
    }

    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定执行此操作",
      success: function(res) {
        if (res.confirm) {
          that.auditRequest(params)
        }
      }
    })
  },


  //不同意
  unAgreedInit: function(e) {
    var showModal = this.data.showModal;
    var item = e.currentTarget.dataset.item
    this.setData({
      showModal: !showModal,
      auditMemo: '',
      unAgreedItem: item
    })
  },


  //监听理由
  onReason: function(e) {
    var inputval = e.detail.value;
    this.setData({
      auditMemo: inputval
    })
  },

  //取消
  cancel: function() {
    var showModal = this.data.showModal;
    this.setData({
      showModal: !showModal,
      auditMemo: '',
      unAgreedItem: ''
    })
  },

  //确定
  confirm: function() {
    if (!this.data.auditMemo) {
      wx.showToast({
        icon: 'none',
        title: '理由不能为空',
      })
      return
    }
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定执行此操作",
      success: function(res) {
        if (res.confirm) {
          that.setData({
            showModal: false,
          })
          //参数
          if (that.data.unAgreedItem) {
            var params = {
              "id": that.data.unAgreedItem.id,
              "stoId": that.data.unAgreedItem.stoId,
              "shared": that.data.unAgreedItem.shared,
              "status": 2,
              "auditMemo": that.data.auditMemo
            }
            that.auditRequest(params);
          }
        }
      }
    })
  },

  //审核
  auditRequest: function(params) {
    var that=this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/storeSharedAudit',
      method: 'POST',
      data: params
    }, (res) => {
      wx.showToast({
        icon: 'succeed',
        title: '操作成功',
      })
      that.getlist()
    })
  }

})